package com.qixiaobao.web.controller.insurecover;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.domain.model.LoginUser;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.common.core.domain.entity.InsureFileCoverRule;
import com.qixiaobao.insure.domain.dto.InsureFileCoverRuleDTO;
import com.qixiaobao.insure.service.IInsureFileCoverRuleService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description 保单遮盖处理
 * @Author yangxm
 * @Date 2025/7/10 10:02
 */
@RestController
@RequestMapping("/insureFileCoverRule")
public class InsureFileCoverRuleController extends BaseController {

    @Autowired
    private IInsureFileCoverRuleService insureFileCoverRuleService;

    /**
     * @Description 保单遮盖规则 列表
     * @Author yangxm
     * @Date 2025/7/10 10:03
     * @param insureFileCoverRuleDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping
    public TableDataInfo list(InsureFileCoverRuleDTO insureFileCoverRuleDTO){
        startPage();
        return getDataTable(insureFileCoverRuleService.selectInsureFileCoverRuleList(insureFileCoverRuleDTO));
    }

    /**
     * @Description 保存
     * @Author yangxm
     * @Date 2025/7/10 10:14
     * @param insureFileCoverRule
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "保单遮盖规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@RequestBody InsureFileCoverRule insureFileCoverRule){
        LoginUser user = getLoginUser();
        if (user == null){
            throw new UtilException("获取当天登陆账号信息失败，请重新登陆！");
        }
        //校验关键字是否存在
        if (insureFileCoverRuleService.checkKeywordUnique(insureFileCoverRule.getKeyword(), insureFileCoverRule.getSupplyName())){
            throw new UtilException("关键字：" + insureFileCoverRule.getKeyword() + "已存在！");
        }
        insureFileCoverRule.setCreateBy(user.getUsername());
        return toAjax(insureFileCoverRuleService.insertInsureFileCoverRule(insureFileCoverRule));
    }

    /**
     * @Description 修改
     * @Author yangxm
     * @Date 2025/7/10 10:20
     * @param insureFileCoverRule
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "保单遮盖规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsureFileCoverRule insureFileCoverRule){
        LoginUser user = getLoginUser();
        if (user == null){
            throw new UtilException("获取当天登陆账号信息失败，请重新登陆！");
        }
        //校验关键字是否存在
        if (insureFileCoverRuleService.checkKeywordUnique2(insureFileCoverRule)){
            throw new UtilException("关键字：" + insureFileCoverRule.getKeyword() + "已存在！");
        }
        insureFileCoverRule.setUpdateBy(user.getUsername());
        return toAjax(insureFileCoverRuleService.updateInsureFileCoverRule(insureFileCoverRule));
    }

    /**
     * @Description 删除
     * @Author yangxm
     * @Date 2025/7/10 10:25
     * @param id
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        return toAjax(insureFileCoverRuleService.deleteInsureFileCoverRuleById(id));
    }

    /**
     * @Description 保单内容遮盖测试
     * @Author yangxm
     * @Date 2025/7/10 10:28
     * @param file
     * @param supplyName
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "保单内容遮盖测试", businessType = BusinessType.OTHER)
    @PostMapping("/pdfCover")
    public AjaxResult pdfCover(MultipartFile file, String supplyName){
        if (file == null || file.isEmpty()){
            throw new UtilException("请上传文件！");
        }
        if (StringUtils.isBlank(supplyName)){
            throw new UtilException("参数：供应商全称不能为空！");
        }
        return AjaxResult.success(insureFileCoverRuleService.pdfCover(file, supplyName));
    }
}
